package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import pailaidai.hc.com.mysvgdemo.R;

/**可以拖动的View
 * Created by Administrator on 2018/4/4.
 */

public class SlidingButton extends TextView {
    private float curX;
    private float curY;
    private Paint paint;
    private Bitmap bitmap;
    private boolean isPicked;
    /*记录点击的位置和上次所在位置的偏移
    * */
    private float offSetX = 0;
    /*记录点击的位置和上次所在位置的偏移
    * */
    private float offSetY = 0;

    public SlidingButton(Context context) {
        super(context);
        initLabeIView();
    }

    public SlidingButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initLabeIView();
    }

    public SlidingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLabeIView();
    }

    private void initLabeIView() {
        paint = new Paint();
        paint.setColor(0xFF888888);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getX() >= curX && event.getX() <= curX + bitmap.getWidth() && event.getY() >= curY && event.getY() <= curY + bitmap.getHeight()) {
                    offSetX = event.getX() - curX;
                    offSetY = event.getY() - curY;
                    invalidate();
                    isPicked = true;
                } else {
                    isPicked = false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isPicked) {
                    curX = event.getX() - offSetX;
                    curY = event.getY() - offSetY;
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isPicked) {
                    curX = event.getX() - offSetX;
                    curY = event.getY() - offSetY;
                    TranslateAnimation translateAnimation = new TranslateAnimation(curX, 0, curY, 0);
                    translateAnimation.setDuration(10);
                    this.startAnimation(translateAnimation);
                    invalidate();
                }
            default:
                break;
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        MeasureSpec.getSize(widthMeasureSpec);
        MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,curX,curY,paint);
    }
}
